package qingke.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qingke.dao.Dao;
import qingke.pojo.Author;

/**
 * Servlet implementation class LikeServlet
 */
@WebServlet("/Like")
public class LikeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LikeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String title = (String) request.getSession().getAttribute("title");
		String authorname = (String) request.getSession().getAttribute("authorname");
		Dao d = new Dao();
		int articleId = d.getArticleId(title, authorname);
		//System.out.println(articleId);
		Author author = (Author) request.getSession().getAttribute("loginuser");
		int authorId = d.getAuthorId(author);
		//System.out.println(authorId);
		int like = d.findLike(authorId, articleId);
		if(like ==1){
			request.getSession().setAttribute("islike", "1");
			response.sendRedirect("blog/article.jsp");
		}else{
			boolean b = d.setLike(authorId, articleId);
			if(b){
				request.getSession().setAttribute("like", "1");
				response.sendRedirect("blog/article.jsp");
			}else{
				request.getSession().setAttribute("like", "0");
				response.sendRedirect("blog/article.jsp");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
